
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.PUB;
using Apps.Models.PUB;
using Apps.IBLL.PUB;
namespace Apps.BLL.PUB
{
	public partial class PUB_DEV_MEDIASERVERBLL: Virtual_PUB_DEV_MEDIASERVERBLL,IPUB_DEV_MEDIASERVERBLL
	{
        

	}
	public class Virtual_PUB_DEV_MEDIASERVERBLL
	{
        [Dependency]
        public IPUB_DEV_MEDIASERVERRepository m_Rep { get; set; }

		public virtual List<PUB_DEV_MEDIASERVERModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<PUB_DEV_MEDIASERVER> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=> a.MEDIASERVER_NAME.Contains(queryStr)
								|| a.USERNAME.Contains(queryStr)
								|| a.IP.Contains(queryStr)
								|| a.WAN_IP.Contains(queryStr)
								|| a.HOME_DIR.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<PUB_DEV_MEDIASERVERModel> GetPagerList(ref GridPager pager, Expression<Func<PUB_DEV_MEDIASERVER, bool>> whereLambda )
        {

            IQueryable<PUB_DEV_MEDIASERVER> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<PUB_DEV_MEDIASERVERModel> GetList(Expression<Func<PUB_DEV_MEDIASERVER, bool>> whereLambda )
        {

            IQueryable<PUB_DEV_MEDIASERVER> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

		public virtual List<PUB_DEV_MEDIASERVERModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<PUB_DEV_MEDIASERVERModel>();
		}

        public virtual List<PUB_DEV_MEDIASERVERModel> CreateModelList(ref IQueryable<PUB_DEV_MEDIASERVER> queryData)
        {

            List<PUB_DEV_MEDIASERVERModel> modelList = (from r in queryData
                                              select new PUB_DEV_MEDIASERVERModel
                                              {
													MEDIASERVER_ID = r.MEDIASERVER_ID,
													MEDIASERVER_NAME = r.MEDIASERVER_NAME,
													MEDIASERVER_TYPE = r.MEDIASERVER_TYPE,
													USERNAME = r.USERNAME,
													PASSWORD = r.PASSWORD,
													IP = r.IP,
													HTTP_PORT = r.HTTP_PORT,
													RTSP_PORT = r.RTSP_PORT,
													SERVER_PORT = r.SERVER_PORT,
													WAN_IP = r.WAN_IP,
													WAN_HTTP_PORT = r.WAN_HTTP_PORT,
													WAN_RTSP_PORT = r.WAN_RTSP_PORT,
													WAN_SERVER_PORT = r.WAN_SERVER_PORT,
													HOME_DIR = r.HOME_DIR,
													NET_STATUS = r.NET_STATUS,
													NET_STATUS_TIMS = r.NET_STATUS_TIMS,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, PUB_DEV_MEDIASERVERModel model)
        {
            try
            {
                PUB_DEV_MEDIASERVER entity = m_Rep.GetById(model.MEDIASERVER_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new PUB_DEV_MEDIASERVER();
               				entity.MEDIASERVER_ID = model.MEDIASERVER_ID;
				entity.MEDIASERVER_NAME = model.MEDIASERVER_NAME;
				entity.MEDIASERVER_TYPE = model.MEDIASERVER_TYPE;
				entity.USERNAME = model.USERNAME;
				entity.PASSWORD = model.PASSWORD;
				entity.IP = model.IP;
				entity.HTTP_PORT = model.HTTP_PORT;
				entity.RTSP_PORT = model.RTSP_PORT;
				entity.SERVER_PORT = model.SERVER_PORT;
				entity.WAN_IP = model.WAN_IP;
				entity.WAN_HTTP_PORT = model.WAN_HTTP_PORT;
				entity.WAN_RTSP_PORT = model.WAN_RTSP_PORT;
				entity.WAN_SERVER_PORT = model.WAN_SERVER_PORT;
				entity.HOME_DIR = model.HOME_DIR;
				entity.NET_STATUS = model.NET_STATUS;
				entity.NET_STATUS_TIMS = model.NET_STATUS_TIMS;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<PUB_DEV_MEDIASERVER, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.PUB_DEV_MEDIASERVER.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, PUB_DEV_MEDIASERVERModel model)
        {
            try
            {
                PUB_DEV_MEDIASERVER entity = m_Rep.GetById(model.MEDIASERVER_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.MEDIASERVER_ID = model.MEDIASERVER_ID;
				entity.MEDIASERVER_NAME = model.MEDIASERVER_NAME;
				entity.MEDIASERVER_TYPE = model.MEDIASERVER_TYPE;
				entity.USERNAME = model.USERNAME;
				entity.PASSWORD = model.PASSWORD;
				entity.IP = model.IP;
				entity.HTTP_PORT = model.HTTP_PORT;
				entity.RTSP_PORT = model.RTSP_PORT;
				entity.SERVER_PORT = model.SERVER_PORT;
				entity.WAN_IP = model.WAN_IP;
				entity.WAN_HTTP_PORT = model.WAN_HTTP_PORT;
				entity.WAN_RTSP_PORT = model.WAN_RTSP_PORT;
				entity.WAN_SERVER_PORT = model.WAN_SERVER_PORT;
				entity.HOME_DIR = model.HOME_DIR;
				entity.NET_STATUS = model.NET_STATUS;
				entity.NET_STATUS_TIMS = model.NET_STATUS_TIMS;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual PUB_DEV_MEDIASERVERModel GetById(object id)
        {
            if (IsExists(id))
            {
                PUB_DEV_MEDIASERVER entity = m_Rep.GetById(id);
                PUB_DEV_MEDIASERVERModel model = new PUB_DEV_MEDIASERVERModel();
                              				model.MEDIASERVER_ID = entity.MEDIASERVER_ID;
				model.MEDIASERVER_NAME = entity.MEDIASERVER_NAME;
				model.MEDIASERVER_TYPE = entity.MEDIASERVER_TYPE;
				model.USERNAME = entity.USERNAME;
				model.PASSWORD = entity.PASSWORD;
				model.IP = entity.IP;
				model.HTTP_PORT = entity.HTTP_PORT;
				model.RTSP_PORT = entity.RTSP_PORT;
				model.SERVER_PORT = entity.SERVER_PORT;
				model.WAN_IP = entity.WAN_IP;
				model.WAN_HTTP_PORT = entity.WAN_HTTP_PORT;
				model.WAN_RTSP_PORT = entity.WAN_RTSP_PORT;
				model.WAN_SERVER_PORT = entity.WAN_SERVER_PORT;
				model.HOME_DIR = entity.HOME_DIR;
				model.NET_STATUS = entity.NET_STATUS;
				model.NET_STATUS_TIMS = entity.NET_STATUS_TIMS;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual PUB_DEV_MEDIASERVERModel GetItem(Expression<Func<PUB_DEV_MEDIASERVER, bool>> whereLambda)
        {
 
                PUB_DEV_MEDIASERVER entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                PUB_DEV_MEDIASERVERModel model = new PUB_DEV_MEDIASERVERModel();
                              				model.MEDIASERVER_ID = entity.MEDIASERVER_ID;
				model.MEDIASERVER_NAME = entity.MEDIASERVER_NAME;
				model.MEDIASERVER_TYPE = entity.MEDIASERVER_TYPE;
				model.USERNAME = entity.USERNAME;
				model.PASSWORD = entity.PASSWORD;
				model.IP = entity.IP;
				model.HTTP_PORT = entity.HTTP_PORT;
				model.RTSP_PORT = entity.RTSP_PORT;
				model.SERVER_PORT = entity.SERVER_PORT;
				model.WAN_IP = entity.WAN_IP;
				model.WAN_HTTP_PORT = entity.WAN_HTTP_PORT;
				model.WAN_RTSP_PORT = entity.WAN_RTSP_PORT;
				model.WAN_SERVER_PORT = entity.WAN_SERVER_PORT;
				model.HOME_DIR = entity.HOME_DIR;
				model.NET_STATUS = entity.NET_STATUS;
				model.NET_STATUS_TIMS = entity.NET_STATUS_TIMS;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<PUB_DEV_MEDIASERVERModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.MEDIASERVER_ID, "MEDIASERVER_ID");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.MEDIASERVER_NAME, "MEDIASERVER_NAME");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.MEDIASERVER_TYPE, "MEDIASERVER_TYPE");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.USERNAME, "USERNAME");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.PASSWORD, "PASSWORD");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.IP, "IP");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.HTTP_PORT, "HTTP_PORT");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.RTSP_PORT, "RTSP_PORT");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.SERVER_PORT, "SERVER_PORT");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.WAN_IP, "WAN_IP");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.WAN_HTTP_PORT, "WAN_HTTP_PORT");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.WAN_RTSP_PORT, "WAN_RTSP_PORT");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.WAN_SERVER_PORT, "WAN_SERVER_PORT");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.HOME_DIR, "HOME_DIR");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.NET_STATUS, "NET_STATUS");
				 excelFile.AddMapping<PUB_DEV_MEDIASERVERModel>(x => x.NET_STATUS_TIMS, "NET_STATUS_TIMS");
 
            //SheetName
            var excelContent = excelFile.Worksheet<PUB_DEV_MEDIASERVERModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new PUB_DEV_MEDIASERVERModel();
						 				  entity.MEDIASERVER_ID = row.MEDIASERVER_ID;
				  entity.MEDIASERVER_NAME = row.MEDIASERVER_NAME;
				  entity.MEDIASERVER_TYPE = row.MEDIASERVER_TYPE;
				  entity.USERNAME = row.USERNAME;
				  entity.PASSWORD = row.PASSWORD;
				  entity.IP = row.IP;
				  entity.HTTP_PORT = row.HTTP_PORT;
				  entity.RTSP_PORT = row.RTSP_PORT;
				  entity.SERVER_PORT = row.SERVER_PORT;
				  entity.WAN_IP = row.WAN_IP;
				  entity.WAN_HTTP_PORT = row.WAN_HTTP_PORT;
				  entity.WAN_RTSP_PORT = row.WAN_RTSP_PORT;
				  entity.WAN_SERVER_PORT = row.WAN_SERVER_PORT;
				  entity.HOME_DIR = row.HOME_DIR;
				  entity.NET_STATUS = row.NET_STATUS;
				  entity.NET_STATUS_TIMS = row.NET_STATUS_TIMS;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<PUB_DEV_MEDIASERVERModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        PUB_DEV_MEDIASERVER entity = new PUB_DEV_MEDIASERVER();
                       						entity.MEDIASERVER_ID = model.MEDIASERVER_ID;
						entity.MEDIASERVER_NAME = model.MEDIASERVER_NAME;
						entity.MEDIASERVER_TYPE = model.MEDIASERVER_TYPE;
						entity.USERNAME = model.USERNAME;
						entity.PASSWORD = model.PASSWORD;
						entity.IP = model.IP;
						entity.HTTP_PORT = model.HTTP_PORT;
						entity.RTSP_PORT = model.RTSP_PORT;
						entity.SERVER_PORT = model.SERVER_PORT;
						entity.WAN_IP = model.WAN_IP;
						entity.WAN_HTTP_PORT = model.WAN_HTTP_PORT;
						entity.WAN_RTSP_PORT = model.WAN_RTSP_PORT;
						entity.WAN_SERVER_PORT = model.WAN_SERVER_PORT;
						entity.HOME_DIR = model.HOME_DIR;
						entity.NET_STATUS = model.NET_STATUS;
						entity.NET_STATUS_TIMS = model.NET_STATUS_TIMS;
 
                        db.PUB_DEV_MEDIASERVER.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
